var $api = require('../../utils/api.js'); //引入api.js页面  
const {
  getHistoryName
} = require('../../utils/api.js');
// pages/search/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: "", //搜索内容
    isHide: true, //是否显示搜索历史
    historyTag: [], //搜索历史
  },


  onShow: function (options) {
    this.getHistoryName();
  },

  getHistoryName() {
    let param;
    $api.getHistoryName(param).then(res => {
      if (res.code == 1) {
        this.setData({
          historyTag: res.data
        })
      }
    })
  },

  onClick: function () {
    var pages = getCurrentPages(); //当前页面
    var prevPage = pages[pages.length - 2]; //上一页面

    if (this.data.search != "") {
      //给首页传值
      if (prevPage.route == "pages/index/index") {
        prevPage.setData({
          searchTag: this.data.search,
          name: this.data.search,
          tagIsHide: true,
          tagShow: true
        })
        wx.navigateBack({
          delta: 1,
        })
      }

      if (prevPage.route == "pages/buying/index") {
        let searchArray = {
          searchTag: this.data.search,
          name: this.data.search,
          tagIsHide: true,
          tagShow: true
        }
        wx.setStorageSync('searchArray', searchArray)
        wx.switchTab({
          url: '../../pages/index/index',
          success: function () {
            var page = getCurrentPages().pop();
            page.getSearchTag();
            page.getGoodsList();
          }
        })
      }

    } else {
      wx.showToast({
        title: '搜索值不能为空',
        icon: 'none'
      })
    }

  },
  //监听搜索框内容
  onChange: function (event) {
    this.setData({
      search: event.detail
    })
  },
  tagSearch: function (e) {
    var pages = getCurrentPages(); //当前页面
    var prevPage = pages[pages.length - 2]; //上一页面
    let name = e.currentTarget.dataset.name;

    // 给首页传值
    if (prevPage.route == "pages/index/index") {
      prevPage.setData({
        searchTag: name,
        name: name,
        tagIsHide: true,
        tagShow: true
      })
      wx.navigateBack({
        delta: 1,
      })
    }

    if (prevPage.route == "pages/buying/index") {
      let searchArray = {
        searchTag: name,
        name: name,
        tagIsHide: true,
        tagShow: true
      }
      wx.setStorageSync('searchArray', searchArray)
      wx.switchTab({
        url: '../../pages/index/index',
        success: function () {
          var page = getCurrentPages().pop();
          page.getSearchTag();
          page.getGoodsList();
        }
      })
    }
  },

  //清空搜索
  clearSearchHistory: function () {
    const that = this;
    wx.showModal({
      title: '提示',
      content: "确认清空历史搜索记录?",
      success: function (res) {
        if (res.confirm) {
          let param;
          $api.clearSearchHistory(param).then(res => {
            if (res.code == 1) {
              that.getHistoryName();
            }
          })
        } else if (res.cancel) {

        }
      },
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})